Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon 5th No.38】为 Paddle 新增 FractionalMaxPool2d / FractionalMaxPool3d API #59130

Closed
wants to merge 27 commits into from

Conversation

megemini
Copy link
Contributor

@megemini megemini commented Nov 19, 2023

PR types

New features

PR changes

APIs

Description

【Hackathon 5th No.38】为 Paddle 新增 FractionalMaxPool2d / FractionalMaxPool3d API

RFC:PaddlePaddle/community#698

涉及文件:

  • paddle/phi/api/yaml/backward.yaml 反向算子描述
  • paddle/phi/api/yaml/ops.yaml 前向算子描述
  • paddle/phi/infermeta/backward.cc 反向算子
  • paddle/phi/infermeta/backward.h 反向算子
  • paddle/phi/infermeta/unary.cc 算子 InferMeta
  • paddle/phi/infermeta/unary.h 算子 InferMeta
  • paddle/phi/kernels/funcs/pooling.cc cpu 算子实现
  • paddle/phi/kernels/funcs/pooling.cu gpu 算子实现
  • paddle/phi/kernels/funcs/pooling.h 算子头文件
  • paddle/phi/kernels/impl/pool_grad_kernel_impl.h
  • paddle/phi/kernels/impl/pool_kernel_impl.h
  • paddle/phi/kernels/pool_grad_kernel.h
  • paddle/phi/kernels/pool_kernel.h
  • python/paddle/nn/__init__.py 暴露 API
  • python/paddle/nn/functional/__init__.py 暴露 API
  • python/paddle/nn/functional/pooling.py API 实现
  • python/paddle/nn/layer/__init__.py 暴露 layer API
  • python/paddle/nn/layer/pooling.py layer API 实现
  • test/legacy_test/test_fractional_max_pool2d.py 2d api/layer 单测
  • test/legacy_test/test_fractional_max_pool3d.py 3d api/layer 单测
  • test/legacy_test/test_pool_max_op.py 算子单测
  • paddle/phi/kernels/xpu/pool_kernel.cc xpu 算子
  • paddle/phi/kernels/xpu/pool_grad_kernel.cc xpu 反向

请评审 ~

Copy link

paddle-bot bot commented Nov 19, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@megemini megemini changed the title 【暂用于 RFC】【Hackathon 5th No.38】为 Paddle 新增 FractionalMaxPool2d / FractionalMaxPool3d API 【Hackathon 5th No.38】为 Paddle 新增 FractionalMaxPool2d / FractionalMaxPool3d API Nov 27, 2023
@megemini
Copy link
Contributor Author

Update 20231128

@Charles-hit

API 应该已经写好了,CI 已经通过 ~ 相较 RFC 有几点改变:

  • 增加了 random_u 参数
    这个参数就是公式 ceiling(α(i + u)) 里面的那个 u,也就是 (0, 1) 范围内的随机数。
    此次实现,这个随机数可以通过 paddle.seed 来固定,虽然可以固定住随机数,但实际上却无法得知 u 是多少 ~
    考虑到能够更好的复现与测试,因此在具体实现的时候增加了这么个 random_u 参数 ~

  • 增加了 xpu 算子的参数
    目前只是增加了参数,从而能够编译通过,不过没有具体内部实现 ~
    xpu 好像没有这部分算子?

  • 参考 adaptive max pool 增加了 test_fractional_max_pool2d.py/test_fractional_max_pool3d.py 的单测文件,由于复用的算子 MaxPoolXdWithIndex 还没有实现 data_format 参数,因此内部的测试用例也暂时注释掉了 ~

请评审 ~ 谢谢!;)

forward : max_pool2d_with_index(Tensor x, int[] kernel_size, int[] strides = {1, 1}, int[] paddings = {0, 0}, bool global_pooling = false, bool adaptive = false) -> Tensor(out), Tensor(mask)
args : (Tensor x, Tensor mask, Tensor out_grad, int[] kernel_size, int[] strides, int[] paddings, bool global_pooling, bool adaptive)
forward : max_pool2d_with_index(Tensor x, int[] kernel_size, int[] strides = {1, 1}, int[] paddings = {0, 0}, bool global_pooling = false, bool adaptive = false, bool fractional = false, float random_u = 0.0) -> Tensor(out), Tensor(mask)
args : (Tensor x, Tensor mask, Tensor out_grad, int[] kernel_size, int[] strides, int[] paddings, bool global_pooling, bool adaptive, bool fractional, float random_u)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这儿直接改变原来phi算子定义会有不兼容升级的问题,新增api的话新增一个phi接口,可以复用已有的max_pool2d_with_index接口即可

forward : max_pool3d_with_index(Tensor x, int[] kernel_size, int[] strides = {1, 1, 1}, int[] paddings = {0, 0, 0}, bool global_pooling = false, bool adaptive = false) -> Tensor(out), Tensor(mask)
args : (Tensor x, Tensor mask, Tensor out_grad, int[] kernel_size, int[] strides, int[] paddings, bool global_pooling, bool adaptive)
forward : max_pool3d_with_index(Tensor x, int[] kernel_size, int[] strides = {1, 1, 1}, int[] paddings = {0, 0, 0}, bool global_pooling = false, bool adaptive = false, bool fractional = false, float random_u = 0.0) -> Tensor(out), Tensor(mask)
args : (Tensor x, Tensor mask, Tensor out_grad, int[] kernel_size, int[] strides, int[] paddings, bool global_pooling, bool adaptive, bool fractional, float random_u)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

@@ -1687,7 +1687,7 @@
backward : matrix_power_grad

- op : max_pool2d_with_index
args : (Tensor x, int[] kernel_size, int[] strides= {1, 1}, int[] paddings = {0, 0}, bool global_pooling = false, bool adaptive = false)
args : (Tensor x, int[] kernel_size, int[] strides= {1, 1}, int[] paddings = {0, 0}, bool global_pooling = false, bool adaptive = false, bool fractional = false, float random_u = 0.0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

@@ -1696,7 +1696,7 @@
backward : max_pool2d_with_index_grad

- op : max_pool3d_with_index
args : (Tensor x, int[] kernel_size, int[] strides = {1, 1, 1}, int[] paddings = {0, 0, 0}, bool global_pooling = false, bool adaptive = false)
args : (Tensor x, int[] kernel_size, int[] strides = {1, 1, 1}, int[] paddings = {0, 0, 0}, bool global_pooling = false, bool adaptive = false, bool fractional = false, float random_u = 0.0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

@@ -658,6 +658,8 @@ void MaxPoolWithIndexGradInferMeta(const MetaTensor& x,
const std::vector<int>& paddings,
bool global_pooling,
bool adaptive,
bool fractional,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

infermeta也需要新增一个复用之前的即可

@@ -2229,6 +2229,8 @@ void MaxPoolWithIndexInferMeta(const MetaTensor& x,
const std::vector<int>& paddings,
bool global_pooling,
bool adaptive,
bool fractional,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

@@ -335,6 +335,8 @@ void MaxPoolWithIndexInferMeta(const MetaTensor& x,
const std::vector<int>& paddings,
bool global_pooling,
bool adaptive,
bool fractional,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

@@ -1571,6 +1571,8 @@ class MaxPool2dWithIndexFunctor<CPUContext, T1, T2> {
const std::vector<int>& strides,
const std::vector<int>& paddings,
bool adaptive,
bool fractional,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接修改原kernel定义也会有不兼容升级的问题,可以新增一个kernel来复用当前kernel

@@ -150,6 +150,8 @@ void MaxPoolWithIndexGradRawKernel(const Context& ctx,
const std::vector<int>& paddings,
bool global_pooling,
bool adaptive,
bool fractional,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这儿不可以直接修改原有kernel签名,会有不兼容升级的问题,新增的算子可以考虑复用这个算子。

from paddle import base
from paddle.base import core


Copy link
Contributor

@Charles-hit Charles-hit Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了方便管理,测试最好继承自optest,需要支持低精度fp16, bfp16等,尽可能覆盖全算子类别。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试有两部分:

  • test/legacy_test/test_pool_max_op.py 这里是测试底层算子的单测文件,继承了 OpTest,覆盖了全算子类别 ~
  • test/legacy_test/test_fractional_max_pool2d.pytest/legacy_test/test_fractional_max_pool3d.py 测试的是 python api ,参考的是 test/legacy_test/test_adaptive_max_pool2d.pytest/legacy_test/test_adaptive_max_pool3d.py ,继承 unittest.TestCase ,这里也要继承 OpTest ?

Copy link
Contributor

@Charles-hit Charles-hit Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试有两部分:

  • test/legacy_test/test_pool_max_op.py 这里是测试底层算子的单测文件,继承了 OpTest,覆盖了全算子类别 ~
  • test/legacy_test/test_fractional_max_pool2d.pytest/legacy_test/test_fractional_max_pool3d.py 测试的是 python api ,参考的是 test/legacy_test/test_adaptive_max_pool2d.pytest/legacy_test/test_adaptive_max_pool3d.py ,继承 unittest.TestCase ,这里也要继承 OpTest ?

这儿需要在yaml中新增两个算子定义,也就是新增两个op,我们针对单个op的测试都是用OpTest去统一管理的,所以继承OpTest是比较合理的测试方式,也便于后续维护。针对API的话,OpTest是可以覆盖到动态图API测试的,check_dygraph开关就是测试动态图API,静态图API测试可以继承自 unittest.TestCase

@megemini
Copy link
Contributor Author

megemini commented Dec 4, 2023

@Charles-hit

关于复用 max_pool2d_with_index 以及相关 kernel,能否帮忙指导一下具体怎么做?

fractional max pool 的 strides 和 ksize 都是跟随 index 改变的,与 adaptive max pool 的流程基本一样,因此,adaptive max pool 的实现也是修改了算子签名、增加了 adaptive 参数后修改 max_pool2d_with_index 相关 kernel 代码从而实现的 ~

如果不修改目前的 kernel 算子签名,如何复用?

还请帮忙看一下 ~

非常感谢!


是否可以在 ops.yaml/backward.yaml 等处新增算子描述,然后在 MaxPool3dWithIndexGradFunctor 等处增加 operator() 的重载?

也就是说,是保留原来的算子,但是内部复用现在增加了 fractional max pool 参数的函数?原来的算子复用现在的,而不是现在复用原来的 ~

@Charles-hit
Copy link
Contributor

@Charles-hit

关于复用 max_pool2d_with_index 以及相关 kernel,能否帮忙指导一下具体怎么做?

fractional max pool 的 strides 和 ksize 都是跟随 index 改变的,与 adaptive max pool 的流程基本一样,因此,adaptive max pool 的实现也是修改了算子签名、增加了 adaptive 参数后修改 max_pool2d_with_index 相关 kernel 代码从而实现的 ~

如果不修改目前的 kernel 算子签名,如何复用?

还请帮忙看一下 ~

非常感谢!

是否可以在 ops.yaml/backward.yaml 等处新增算子描述,然后在 MaxPool3dWithIndexGradFunctor 等处增加 operator() 的重载?

也就是说,是保留原来的算子,但是内部复用现在增加了 fractional max pool 参数的函数?原来的算子复用现在的,而不是现在复用原来的 ~

yaml处需要新增算子描述,新增两个算子,如果想复用原有kernel的话确实可以考虑你说的那种情况,增加一个重载,不要改变原有kernel的参数以及行为就好。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants